package com.bangcommunity.bbframe.test.web;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppClassLoader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

/**
 * 创建Jetty Server的工厂类.
 * 
 * @author calvin
 */
public class QtJettyFactory {
    static Logger logger = LoggerFactory.getLogger(QtJettyFactory.class);

    private QtJettyFactory() {
    }

    /**
     * 创建用于开发运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
     */
    public static Server buildNormalServer(String webapp, int port, String contextPath, String[] classpaths) {
        Server server = new Server(port);
        WebAppContext webContext = new WebAppContext(webapp, contextPath);
        webContext.setThrowUnavailableOnStartupException(true);
        server.setHandler(webContext);
        server.setStopAtShutdown(true);
        try {
            WebAppClassLoader webAppClassLoader = new WebAppClassLoader(webContext);
            webContext.setClassLoader(webAppClassLoader);
            if (null != classpaths) {
                for (String cl : classpaths) {
                    try {
                        webAppClassLoader.addClassPath(cl);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return server;
    }

    public static Server startNormalServer(String project, int port, String contextPath) {
        if (null != project && project.length() > 0 && !project.endsWith("/")) {
            project = project + "/";
        }
        // Server server = MyJettyFactory.buildNormalServer(project+"src/main/webapp", port,
        // contextPath, new String[]{
        // project+"build/classes/main"
        // , project+"build/classes/test"
        // , project+"build/resources/main"
        // , project+"build/resources/test"
        // });
        Server server = buildNormalServer(project + "src/main/webapp", port, contextPath, null);
        boolean isSuccess = true;
        try {
            server.start();
        } catch (Exception e) {
            isSuccess = false;
            logger.error("start server excpetion", e);
            try {
                server.stop();
            } catch (Exception e1) {
                logger.error("stop server excpetion", e);
            }
        }
        logger.info("===============================================================");
        if (isSuccess) {
            logger.info("== Server  running at " + project + ":,url: localhost:" + port + contextPath);
        } else {
            logger.info("!!!! Server start fail !!!!");
        }
        logger.info("===============================================================");
        return server;
    }

}
